//
//  ZFJPopView.m
//  GSDemo
//
//  Created by ZhangFujie on 2022/10/12.
//

#import "ZFJPopView.h"
#import "UIColor+ZFJPopCate.h"

///
#define kZFJScreenWidth [UIScreen mainScreen].bounds.size.width
///
#define kZFJScreenHeight [UIScreen mainScreen].bounds.size.height

@interface ZFJPopView ()<UIGestureRecognizerDelegate>

///
@property (nonatomic,strong) UIView *topView;
///
@property (nonatomic,strong) UIView *popView;
///
@property (nonatomic,assign) CGFloat popViewW;
///
@property (nonatomic,assign) CGFloat popViewH;
///
@property (nonatomic,strong) UITapGestureRecognizer *backgroundTap;

@end

@implementation ZFJPopView

- (instancetype)initWithPopView:(UIView *)popView topView:(UIView *)topView{
    if (self = [super initWithFrame:[UIScreen mainScreen].bounds]) {
        [self initData];
        [self uiConfig:popView topView:topView];
    }
    return self;
}

- (instancetype)initWithPopView:(UIView *)popView {
    if (self = [self initWithPopView:popView topView:self.zfj_keyWindow]) {
        ///
    }
    return self;
}

- (void)initData{
    self.disableBackgroundClick = NO;
    self.animateStyle = ZFJAnimateFromTop;
}

- (void)uiConfig:(UIView *)popView topView:(UIView *)topView{
    if(topView == nil){
        topView = self.zfj_keyWindow;
    }
    self.topView = topView;
    self.popView = popView;
    self.popViewH = popView.frame.size.height;
    self.popViewW = popView.frame.size.width;
    
    self.backgroundColor = [UIColor colorWithHexString:@"000000" withAlpha:0.6];
}

- (void)setDisableBackgroundClick:(BOOL)disableBackgroundClick{
    _disableBackgroundClick = disableBackgroundClick;
    if(_disableBackgroundClick){
        [self removeGestureRecognizer:self.backgroundTap];
    }else{
        [self addGestureRecognizer:self.backgroundTap];
    }
}

- (void)backgroundTapClick:(UITapGestureRecognizer *)tap{
    if(self.popView != nil){
        [self hidePopAnimationView];
    }
}

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch{
    if ([touch.view isDescendantOfView:self.popView] ) {
        return NO;
    }
    return YES;
}

/// 显示弹窗动画视图
- (void)showPopAnimationView{
    if(self.viewWillAppear){
        self.viewWillAppear();
    }
    if(self.animateStyle == ZFJAnimateFromTop){
        self.popView.frame = CGRectMake((kZFJScreenWidth - self.popViewW) / 2, - self.popViewH , self.popViewW, self.popViewH);
    }else if(self.animateStyle == ZFJAnimateFromLeft){
        self.popView.frame = CGRectMake(-self.popViewW, self.center.y - self.popViewH/2 , self.popViewW, self.popViewH);
    }else if(self.animateStyle == ZFJAnimateFromRight){
        self.popView.frame = CGRectMake(kZFJScreenWidth, self.center.y - self.popViewH/2 , self.popViewW, self.popViewH);
    }else if(self.animateStyle == ZFJAnimateFromBottom){
        self.popView.frame = CGRectMake((kZFJScreenWidth - self.popViewW) / 2, kZFJScreenHeight , self.popViewW, self.popViewH);
    }else if(self.animateStyle == ZFJAnimateByScale){
        self.popView.center = self.center;
        self.popView.transform = CGAffineTransformMakeScale(0.4f, 0.4f);
    }else{
        self.popView.center = self.center;
    }
    
    ZFJPopWeakSelf;
    [UIView animateWithDuration:0.5 delay:0.0 usingSpringWithDamping:0.5 initialSpringVelocity:0.5 options:UIViewAnimationOptionCurveLinear animations:^{
        if(weakSelf.animateStyle == ZFJAnimateByScale){
            weakSelf.popView.transform = CGAffineTransformMakeScale(1.0f, 1.0f);
        }
        weakSelf.popView.center = weakSelf.center;
    } completion:^(BOOL finished) {
        if(weakSelf.viewDidAppear){
            weakSelf.viewDidAppear();
        }
    }];
    
    [self.topView addSubview:self];
    [self addSubview:self.popView];
}

/// 关闭弹窗动画视图
- (void)hidePopAnimationView{
    if(self.viewWillDisappear){
        self.viewWillDisappear();
    }
    __weak typeof(self) weakSelf = self;
    [UIView animateWithDuration:0.3 animations:^{
        if (weakSelf.animateStyle == ZFJAnimateFromTop){
            weakSelf.popView.frame = CGRectMake((kZFJScreenWidth - weakSelf.popViewW) / 2, kZFJScreenHeight , weakSelf.popViewW, weakSelf.popViewH);
        }else if(weakSelf.animateStyle == ZFJAnimateFromLeft){
            weakSelf.popView.frame = CGRectMake(kZFJScreenWidth, weakSelf.center.y - weakSelf.popViewH / 2, weakSelf.popViewW, weakSelf.popViewH);
        }else if (weakSelf.animateStyle == ZFJAnimateFromRight){
            weakSelf.popView.frame = CGRectMake(- weakSelf.popViewW, weakSelf.center.y - weakSelf.popViewH/2 , weakSelf.popViewW, weakSelf.popViewH);
        }else if (weakSelf.animateStyle == ZFJAnimateFromBottom){
            weakSelf.popView.frame = CGRectMake((kZFJScreenWidth - weakSelf.popViewW) / 2, - weakSelf.popViewH , weakSelf.popViewW, weakSelf.popViewH);
        }else if (weakSelf.animateStyle == ZFJAnimateByScale){
            weakSelf.popView.center = weakSelf.center;
            weakSelf.popView.transform = CGAffineTransformMakeScale(0.01f, 0.01f);
        }
    } completion:^(BOOL finished) {
        if(weakSelf.viewDidDisappear){
            weakSelf.viewDidDisappear();
        }
        [weakSelf removeFromSuperview];
    }];
}

- (UIWindow *)zfj_keyWindow{
    UIWindow *keyWindow = nil;
    NSArray *windows = [[UIApplication sharedApplication] windows];
    for (UIWindow  *window in windows) {
        if (window.isKeyWindow) {
            keyWindow = window;
            break;
        }
    }
    return keyWindow;
}

- (UITapGestureRecognizer *)backgroundTap{
    if(_backgroundTap == nil){
        _backgroundTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(backgroundTapClick:)];
        _backgroundTap.delegate = self;
    }
    return _backgroundTap;
}

- (void)setBackgroundColor:(UIColor *)backgroundColor{
    if(backgroundColor == nil){
        backgroundColor = [UIColor colorWithHexString:@"000000" withAlpha:0.6];
    }
    [super setBackgroundColor:backgroundColor];
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
